package com.groupbuying.common.baseutils.baseframework4plugins.utils;

import com.groupbuying.common.baseutils.baseframework4plugins.servicelocator.ServiceLocator;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @anthor zmlmagic
 * @date 2018/12/27 9:17 AM
 * @company 蒙泰互联网公司
 * @des 备注说明
 **/

public class ModelHelper {
    private ModelHelper() {
    }

    public static Class getModelClass(Class cls, String modulePathName, int index) {
        return getModelClass(cls, modulePathName, index, 0);
    }

    public static Class getQueryModelClass(Class cls, String modulePathName, int index) {
        return getModelClass(cls, modulePathName, index, 1);
    }

    private static Class getModelClass(Class cls, String modulePathName, int index, int initParamType) {
        try {
            ParameterizedType pt = (ParameterizedType) cls.getGenericSuperclass();
            Type t = pt.getActualTypeArguments()[index];
            String name = t.getTypeName();
            return Class.forName(name);
        } catch (Exception var7) {
            if (!StringUtils.isEmpty(modulePathName)) {
                ServiceLocator.getInstance();
                return (Class) ((List) ServiceLocator.getMapInitParams().get(modulePathName)).get(initParamType);
            } else {
                return null;
            }
        }
    }

    public static String getModulePathName(Class cls) {
        String packageStr = "";

        try {
            packageStr = cls.getPackage().getName();
            int index1 = packageStr.indexOf(".plugins.");
            int index2 = packageStr.indexOf(".plugins.", index1 + 1);
            return packageStr.substring(index1 + ".plugins.".length(), index2);
        } catch (Exception var4) {
            return null;
        }
    }

    public static void main(String[] args) {
    }
}

